package com.ljt.realtimebusSS.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ljt.realtimebusSS.domain.common.BaseResponse;
import com.ljt.realtimebusSS.domain.common.ResultUtils;
import com.ljt.realtimebusSS.domain.entity.*;
import com.ljt.realtimebusSS.domain.vo.ArrivalTimeVO;
import com.ljt.realtimebusSS.domain.vo.BusRouteVO;
import com.ljt.realtimebusSS.service.*;
import com.ljt.realtimebusSS.service.impl.BusRouteServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
public class Controller {


    private final BusRouteServiceImpl busRouteService;
    private final ArrivalTimeService arrivalTimeService;
    private final BusVehicleService busVehicleService;
    private final BusStopService busStopService;
    private final RouteStopService routeStopService;

    @GetMapping("/allline")
    public BaseResponse getAllLine(){
        // 获取站点详细信息
        // 联表查询
        List<RouteStop> routeStopList = routeStopService.list();
        routeStopList.sort(Comparator.comparing(RouteStop::getStopOrder));
        // 所有的routestop对象，进行对routeId分组。这个map中一个routeId关联表id->关联表对象列表
        Map<Integer, List<RouteStop>> integerListMap = routeStopList.stream().collect(Collectors.groupingBy(RouteStop::getRouteId));


        // 查询所有的站点，做成一个map，
        Map<Integer, String> stopIdStopNameMap = busStopService.list().stream().collect(Collectors.toMap(e -> e.getStopId(), e -> e.getStopName()));
        // 对每一个路线id->站点名称列表
        Map<Integer, List<String>> routeIdStopNameMap = integerListMap.entrySet().stream().
                collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()
                        .stream()
                        .map(routeStop -> stopIdStopNameMap.get(routeStop.getStopId()))
                        .collect(Collectors.toList())
                ));

        // 获取路线详细信息
        List<BusRoute> busRouteList = busRouteService.list();

        // 对一个路线对象塞一个站点名称列表
        // 一条一条塞回去
        List<BusRouteVO> collect = busRouteList.stream().map(b -> {
            BusRouteVO busRouteVO = new BusRouteVO();
            BeanUtil.copyProperties(b, busRouteVO);
            busRouteVO.setStopNameList(routeIdStopNameMap.get(b.getRouteId()));
            return busRouteVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }

    @GetMapping("/arrival/{stopName}")
    public BaseResponse getRealPosition(@PathVariable("stopName") String stopName){
        Integer stopId = busStopService.getOne(Wrappers.lambdaQuery(BusStop.class)
                        .eq(BusStop::getStopName, stopName))
                .getStopId();

        List<ArrivalTime> list = arrivalTimeService.lambdaQuery()
                .eq(ArrivalTime::getStopId,stopId)
                .list();

        // arrivalTime列表中取出所有的vehicleId
        Set<Integer> vehicleIdSet = list.stream()
                .map(ArrivalTime::getVehicleId)
                .collect(Collectors.toSet());

        if (vehicleIdSet.isEmpty()) {
            return ResultUtils.success(null);
        }

        // 查询所有的线路表,做成map,根据routeId,可以取出routeName
        Map<Integer, String> routeMap = busRouteService.list()
                .stream()
                .collect(Collectors.toMap(BusRoute::getRouteId, e -> e.getRouteName()));


        // 根据vehicleId集合查询vehicle,做成map,根据vehicleId,可以取出routeName
        Map<Integer, String> vehicleMap = busVehicleService.list(
                Wrappers.lambdaQuery(BusVehicle.class)
                        .in(BusVehicle::getVehicleId, vehicleIdSet))
                .stream()
                .collect(Collectors.toMap(BusVehicle::getVehicleId, e -> routeMap.get(e.getRouteId())));

        // 对原来的arrivalTime列表进行做成arrivalTimeVO列表
        List<ArrivalTimeVO> collect = list.stream().map(arrivalTime -> {
            ArrivalTimeVO arrivalTimeVO = new ArrivalTimeVO();
            BeanUtil.copyProperties(arrivalTime, arrivalTimeVO);
            arrivalTimeVO.setRouteName(vehicleMap.get(arrivalTime.getVehicleId()));
            return arrivalTimeVO;
        }).collect(Collectors.toList());

        return ResultUtils.success(collect);
    }

    @GetMapping("/position/{routeName}")
    public BaseResponse getPosition(@PathVariable("routeName") String routeName){
        Integer routeId = busRouteService.getOne(Wrappers.lambdaQuery(BusRoute.class)
                        .like(BusRoute::getRouteName, routeName))
                .getRouteId();

        List<BusVehicle> list = busVehicleService.list(
                Wrappers.lambdaQuery(BusVehicle.class)
                        .eq(BusVehicle::getRouteId, routeId));
        return ResultUtils.success(list);
    }
}
